package com.dayatang.android.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Db.ProjectSearchServiceDb;

public class ListViewDataConfig extends BaseConfig{
	
	public static String isNotProjectStatusData(String url, Context context) {
		try {
			return getHttpData(context,url);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<HashMap<String, Object>> getProjectStatusData(String data, Context context) { 
		
		ProjectSearchServiceDb projectSearchServiceDb = new ProjectSearchServiceDb(context);
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		projectSearchServiceDb.delAllProject();
		
		try {
			
			JSONArray jsonArray = new JSONArray(data);
			
			for (int i = 0; i < jsonArray.length(); i++) {  
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);  
				
				HashMap<String, Object> map = new HashMap<String, Object>();
			    map.put("ItemProjectName",jsonObject.getString("projName"));
			    map.put("ItemProjectOutput",jsonObject.getString("output") + context.getString(R.string.subproject_finish_output_unit));
			    map.put("ItemProjectId", jsonObject.getString("id"));
				map.put("ItemProjectExpenditure", jsonObject.getString("expenditure") + context.getString(R.string.subproject_finish_output_unit));
				map.put("ItemProjectReceipt", jsonObject.getString("receipt") + context.getString(R.string.subproject_finish_output_unit));
			    map.put("ItemProjectStatus", statusToChinese(jsonObject.getString("status")));
				
			    projectSearchServiceDb.saveProject(JsonDataToProjectData.getProject(jsonObject));
				
			    listItem.add(map);
		    }  
			return listItem;
		} catch (JSONException e) {
			e.printStackTrace();  
			Toast.makeText(context, context.getString(R.string.project_search_failure), Toast.LENGTH_LONG).show();  
		}
			
		return null;
	}
	
	public static List<HashMap<String, Object>> getSubProjectStatusData(String data, Context context) { 
		
		ProjectSearchServiceDb projectSearchServiceDb = new ProjectSearchServiceDb(context);
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		projectSearchServiceDb.delAllSubProject();
		Log.i("123", "start");
		try {
			Log.i("123", data);
			JSONArray jsonArray = new JSONArray(data);
			
			for (int i = 0; i < jsonArray.length(); i++) {  
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);  
				
				HashMap<String, Object> map = new HashMap<String, Object>();
			    map.put("ItemSubProjectName",jsonObject.getString("projName"));
			    map.put("ItemCurrentWorkStep",jsonObject.getString("currentWorkStep"));
			    map.put("ItemSubProjectId", jsonObject.getString("id"));
			    map.put("ItemSubProjectOutput", jsonObject.getString("output") + context.getString(R.string.subproject_finish_output_unit));
				
			    projectSearchServiceDb.saveSubProject(JsonDataToProjectData.getSubProject(jsonObject));
				
			    listItem.add(map);
		    }  
			return listItem;
		} catch (JSONException e) {
			Log.i("123", e.toString());
			e.printStackTrace();  
			Toast.makeText(context, context.getString(R.string.project_search_failure), Toast.LENGTH_LONG).show();  
		}
			
		return null;
	}
	
	public static List<HashMap<String, Object>> getOverTimeStepData(String data, Context context) { 
		
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		
		try {
			JSONArray jsonArray = ((JSONObject) new JSONArray(data).get(0)).getJSONArray("overtimeStep");  
			for (int i = 0; i < jsonArray.length(); i++) {  
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);  
				HashMap<String, Object> map = new HashMap<String, Object>();
			    map.put("ItemSubProjectName",jsonObject.getString("name"));
			    map.put("ItemSubProjectWorkStep",jsonObject.getString("workStepName"));
			    map.put("ItemSubProjectWorkStepResponsible", jsonObject.getString("responsible"));
				map.put("ItemSubProjectOverTimeDay", jsonObject.getString("overDay") + context.getString(R.string.pm_day));
			    listItem.add(map);
			}
			return listItem;
		} catch (JSONException e) {
			e.printStackTrace();  
		}
		return null;
	}
	
	public static List<HashMap<String, Object>> getMyTaskpData(String data, Context context) { 
		
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		
		try {
			JSONArray jsonArray = new JSONArray(data);
			
			for (int i=0; i < jsonArray.length(); i++) {
				
				JSONObject jsonObject =(JSONObject) jsonArray.get(i);
				
			    HashMap<String, Object> map = new HashMap<String, Object>();
			    map.put("SubProjectId", jsonObject.getString("subProjectId"));
			    map.put("ProjName", jsonObject.getString("projName"));
			    map.put("SubProjectName", jsonObject.getString("subprojName"));
			    map.put("ResponsibleName", jsonObject.getString("responsibleName"));
				map.put("WorkStepName", jsonObject.getString("workStepName"));
				map.put("ProgressStatus", jsonObject.getString("progressStatus"));
				
			    listItem.add(map); 
				
			}
			return listItem;
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String statusToChinese(String status) {
		if (status.equals("draft")) {
			return "草稿";
		} else if (status.equals("approvaling")) {
			return "正在审批";
		} else if (status.equals("unaccess")) {
			return "未通过";
		} else if (status.equals("access")) {
			return "通过";
		} else if (status.equals("design")) {
			return "设计";
		} else if (status.equals("start")) {
			return "开工";
		}
		return null;
	}
	
}
